﻿#include "MaterialDensityWidget.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDensity.h"
#include "ui_MaterialDensityWidget.h"
#include <QAbstractItemDelegate>
#include <QCloseEvent>
#include <QMessageBox>
#include <QStandardItemModel>
#include <QTableWidgetItem>

namespace GUI
{
    MaterialDensityWidget::MaterialDensityWidget(EventOper::ParaWidgetInterfaceOperator * oper,Interface::FITKAbaMaterialDensity* item, QWidget* parent) :
        _densityItem(item), MaterialWidgetBase(oper,parent), _ui(new Ui::MaterialDensityWidget)
    {
        if (_densityItem == nullptr)return;
        _ui->setupUi(this);

        initUi();
    }

    MaterialDensityWidget::~MaterialDensityWidget()
    {
        //析构清除界面
        delete _ui;
    }

    bool MaterialDensityWidget::checkValue()
    {
//        auto checkValue = [=](QString str, int row, int column)->bool {
//            //判断数据是否有效
//            bool isOk = false;
//            str.toDouble(&isOk);
//            if (isOk == true)return true;

//            QString message = QString(tr("Tabular data for at least one option or suboption\n has either blank or zero valued row(s).\n"));
//            message = message + QString(tr("Error: Density row: %1\n").arg(row + 1));
//            message = message + QString(tr("Error: Density column: %1\n").arg(column + 1));
//            QMessageBox::warning(this, tr("StructualApp"), message);
//            return true;
//        };

        bool isTemp = _ui->checkBox_temp->isChecked();
        //强制失去焦点，否则获取数据有错误
        _ui->tableWidget->setEnabled(false);
        _ui->tableWidget->setEnabled(true);
        int rowNum = _ui->tableWidget->rowCount();
        int colNum = _ui->tableWidget->columnCount();
        for (int i = 0; i < rowNum; i++) {
            for (int j = 0; j < colNum; j++) {
                QString value = "";
                QTableWidgetItem* item = _ui->tableWidget->item(i, j);
                if (item == nullptr)continue;
                value = item->text();
                if (j == 1 && isTemp == false) {
                    continue;
                }
                else {
                    //if (!checkValue(value, i, j))return false;
                }
            }
        }
        return true;
    }

    bool MaterialDensityWidget::saveValue()
    {
        if (_densityItem == nullptr)return false;
        //强制失去焦点，否则获取数据有错误
        _ui->tableWidget->setEnabled(false);

        //温度参数存储
        bool isTemp = _ui->checkBox_temp->isChecked();
        _densityItem->setUseTemperatureDependentData(isTemp);

        //TableWidget中数据存储
        int rowNum = _ui->tableWidget->rowCount();
        _densityItem->setNumberOfRows(rowNum);
        for (int i = 0; i < rowNum; i++) {
            QTableWidgetItem* item = nullptr;
            double value = 0;

            item = _ui->tableWidget->item(i, 0);
            if (!item)value = 0;
            else {
                QString text = item->text();
                QRegExp regex("([0-9\\.]+)");  // 匹配数字和小数点
                if (regex.indexIn(text) != -1) {
                    value = regex.cap(1).toDouble();  // 提取到的数字部分
                }
                //value = item->text().toDouble();
            }
            _densityItem->setMassDensity(value, i);
            
            if (isTemp) {
                item = _ui->tableWidget->item(i, 1);
                if (!item) value = 0;
                else value = item->text().toDouble();
            }
            else value = 0;
            _densityItem->setTemperature(value, i);
        }

        return true;
    }

    void MaterialDensityWidget::initUi()
    {
        _ui->tableWidget->addTableRowAndItem(0);

        //设置温度是否显示
        bool isTemp = _densityItem->getUseTemperatureDependentData();
        _ui->checkBox_temp->setChecked(isTemp);
        if (isTemp)_ui->tableWidget->showColumn(1);
        else _ui->tableWidget->hideColumn(1);

        _ui->tableWidget->setIsAloneRow(true);
        _ui->tableWidget->setAppendLineByEnterPressed(false);

        //显示表格参数
        int rowNum = _densityItem->getNumberOfRows();
        for (int i = 0; i < rowNum; i++) {
            QTableWidgetItem* item = new QTableWidgetItem(QString::number(_densityItem->getMassDensity(i)));
            _ui->tableWidget->setItem(i, 0, item);
            item = new QTableWidgetItem(QString::number(_densityItem->getTemperature(i)));
            _ui->tableWidget->setItem(i, 1, item);
        }
    }

    void MaterialDensityWidget::on_checkBox_temp_clicked(bool checked)
    {
        if (checked)_ui->tableWidget->showColumn(1);
        else _ui->tableWidget->hideColumn(1);
    }
}  // namespace GUI
